//
//  AlbumDetailsViewController.m
//  DAlbum
//
//  Created by David Qiu on 15/9/15.
//  Copyright (c) 2015年 David Qiu. All rights reserved.
//

#import "AlbumDetailsViewController.h"
#import "AlbumItem.h"
#import "AlbumsManager.h"



@interface AlbumDetailsCollectionViewCell : UICollectionViewCell

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end


@implementation AlbumDetailsCollectionViewCell

@end



@interface AlbumDetailsViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (strong, nonatomic) NSString* albumName;
@property (strong, nonatomic) NSMutableArray* albumItems;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end


@implementation AlbumDetailsViewController

#pragma mark - View Control

- (void)viewDidLoad {
    [super viewDidLoad];

    // Initialize: Collection view
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    // Initialize: Editing status
    [self setEditing:NO];
    
    // Initialize: Pictures collection
    [self refreshAlbumItems];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    NSLog(@"[ INFO ] Set editing status. (status: %d)", editing);
    
    // UI: Edit button
    UIBarButtonSystemItem systemItem = (self.editing) ? (UIBarButtonSystemItemDone) : (UIBarButtonSystemItemEdit);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem target:self action:@selector(buttonEditClicked:)];
}

#pragma mark - Internal Operations

- (void) refreshAlbumItems {
    // Refresh album name
    self.albumName = [AlbumsManager defaultManager].selectedAlbumName;
    self.navigationItem.title = self.albumName;
    
    // Check the array
    if (self.albumItems == nil) {
        self.albumItems = [[NSMutableArray alloc] init];
    }
    
    // Remove all original items
    [self.albumItems removeAllObjects];
    
    // Fetch the latest pictures list
    NSArray* pictures = [[AlbumsManager defaultManager] pictures];
    
    // Update the album items
    for (NSUInteger i=0; i<pictures.count; ++i) {
        [self.albumItems addObject:[[AlbumItem alloc] initWithFileName:[pictures objectAtIndex:i]]];
    }
    
    // Reload collection view
    [self.collectionView reloadData];
}


#pragma mark - Button Events

- (IBAction) buttonEditClicked:(id)sender {
    // Toggle editing status
    [self setEditing:(!self.editing) animated:YES];
}


#pragma mark - Collection View Data Source

- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.albumItems.count;
}

- (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    // Construct the current cell from the reusable cell
    AlbumDetailsCollectionViewCell* cell = (AlbumDetailsCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"AlbumDetailsReusableCell" forIndexPath:indexPath];
    
    // Load the image data
    NSString* fileName = ((AlbumItem *)[self.albumItems objectAtIndex:[indexPath row]]).fileName;
    NSString* albumName = self.albumName;
    UIImage* image = [[AlbumsManager defaultManager] thumbnailByFilename:fileName albumName:albumName];
    if (image) {
        cell.imageView.image = image;
    } else {
        NSLog(@"[ ERROR ] Picture does not exist. (fileName: \"%@\", albumName: \"%@\")", fileName, albumName);
    }
    
    // Return the current cell
    return cell;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
